<?php

namespace app\api\controller;

use app\api\model\Setting as SettingModel;
use app\api\model\user\Paper;
use app\common\model\user\Application;
use app\common\model\user\PaperTopic;

/**
 * 默认控制器
 * Class Index
 * @package app\api\controller
 */
class Index extends Controller
{
    public function index()
    {
        var_dump(time());
//        if ($this->isMobile()) {
//            $url = "/m";
//        } else {
//        $url = "/web";
////        }
//        header("Location: $url");
//        die;
    }

    public function isMobile()
    {
        $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
        $mobileDevices = array(
            'android',
            'blackberry',
            'iphone',
            'ipad',
            'ipod',
            'iemobile',
            'opera mini',
            'palmos',
            'palmsource',
            '240x320',
            '480x640',
            '768x1024'
        );

        foreach ($mobileDevices as $device) {
            if (strstr($userAgent, strtolower($device))) {
                return true;
            }
        }
        return false;
    }

    public function config(string $key)
    {
        $values = SettingModel::getItem('examination');
        if ($key === 'cnqmnr' && !empty($values[$key])) {
            return $this->renderSuccess([
                'html'=>htmlspecialchars_decode($values[$key])
            ]);
        }
        if ($key === 'ysbhzc' && !empty($values[$key])) {
            return $this->renderSuccess([
                'html'=>htmlspecialchars_decode($values[$key])
            ]);
        }
        if ($key === 'yhfwxy' && !empty($values[$key])) {
            return $this->renderSuccess([
                'html'=>htmlspecialchars_decode($values[$key])
            ]);
        }
        return $this->renderSuccess();
    }

    public function paperpreview($paper_id)
    {
        $paper = Paper::detail($paper_id, ['user', 'job', 'subject_level', 'examsign_file']);
        if (!$paper) {
            return false;
        }
        $paper->application = (new Application())->with(['job', 'subject_level', 'face_file'])->where([
            'application_id' => $paper->application_id
        ])->find();
        //判断题
        $t1 = (new PaperTopic())->where([
            'paper_id' => $paper_id,
            'qt_type' => 1,
        ])->select();
        $t2 = (new PaperTopic())->where([
            'paper_id' => $paper_id,
            'qt_type' => 2,
        ])->select();
        $t3 = (new PaperTopic())->where([
            'paper_id' => $paper_id,
            'qt_type' => 3,
        ])->select();

        return view('', ['paper' => $paper, 't1' => $t1, 't2' => $t2, 't3' => $t3]);
    }


}
